! 函数作为函数的参数
module callback_func

    implicit none
    abstract interface              ! 抽象接口
        pure subroutine fcn(x, y, z)
            integer, intent(in) :: x, y
            integer, intent(out) :: z
        end subroutine fcn
    end interface

contains

    pure subroutine do_something(func, x, y, z)
        procedure(fcn) :: func      ! 函数作为参数
        integer, intent(in) :: x, y
        integer, intent(out) :: z

        call func(x, y, z)

    end subroutine do_something

end module callback_func

program main

    use callback_func
    implicit none

    integer :: x, y, z

    x = 1; y = 2
    call do_something(add, x, y, z)
    print *, z                      ! 3

    call do_something(subtract, x, y, z)
    print *, z                      ! -1

contains

    pure subroutine add(x, y, z)
        integer, intent(in) :: x, y
        integer, intent(out) :: z
        z = x + y
    end subroutine add

    pure subroutine subtract(x, y, z)
        integer, intent(in) :: x, y
        integer, intent(out) :: z
        z = x - y
    end subroutine subtract

end program main
